Macromedia - Developer Center: Creating a Flash Element
Table of Contents
- Introduction
- Preparing a Flash Component for a life as a Flash Element
- Creating the Extension
Preparing a Flash Component for Life as a Flash Element
A Macromedia Flash MX 2004 component is almost ready to be a Flash element. There are a few subtle changes you have to make to the meta data in the class file however. Meta data are special "tags" placed inside the source code; programs that use the component can use the information in meta data for special purposes. For instance, [Inspectable] meta data exposes properties as component parameters in Macromedia Flash MX 2004. Dreamweaver uses the same meta data so you can make similar settings for a Flash element. Here is a quick overview of what you need to do:
- Replace double quotes (") in meta data with single quotes (').
- Move [Inspectable] tags to the getter methods rather setter methods.
- Change link related types to URI instead of String.
- Add [ArrayProps] tags to array properties.
None of these changes will keep your component from functioning in Flash MX 2004, it will continue working there just fine. Yet these changes are necessary for your component to work as a Flash Element also.
Before you get into updating the FooterNav component, let me explain first why these changes are necessary.
Single Quotes
You don't really have to understand why this change is necessary—you can just do it. But since you're already reading this section, you're interested in the why, so here you go.
This is a bit tricky to explain, so bear with me. A Flash element is embedded into an HTML page. The solution Macromedia chose to pass the settings you make to the Flash element is to set a single variable that contains all your settings in the embed/object tag as part of the FlashVars attribute/param. Are you still with me? This attribute is defined in the HTML using double quotes. Hence the easiest way to use quotes inside the variable is by using single quotes; you always use the "other type" of quotes inside a string contained with one type of quotes.
An example to illustrate this:
meta data in source code
public function set captionFont( myFont:String ):Void {
__captionFont = myFont;
invalidate();
}
[Inspectable(type='String', defaultValue='Verdana' )]
public function get captionFont():String {
return __captionFont;
}
Result in the <OBJECT> tag:
<param name="FlashVars" value="flashlet={captionFont:'Verdana'}">
and in the <EMBED> tag:
flashvars="flashlet={captionFont:'Verdana'}"
You look at this code if you look at the source code after inserting a Flash element into your page. A Flash element has some special added parsing code that makes a real object out of this variable.
Note:You don't need to replace all double quotes in your code with single quotes. This is for meta data tags only!
Placement of [Inspectable] Tags
The parsing code of the Flash element needs to read two pieces of
information about each property: name and type. It gathers this information
with the get method of a property. The set method
of a property contains additional information: a parameter with a name
and type. Since it's a method, it could theoretically have more than
one parameter. Either way, that additional information is unnecessary
for being able to use the property, and worse, seems to confuse the
parsing code at the moment.
To be safe, just use your Meta Data with the get method.
Example:
public function set captionText( caption:String ) {
__captionText = caption;
invalidate();
}
[Inspectable(type='String', defaultValue='caption goes here' )]
public function get captionText():String {
return __captionText;
}
Type Setting for Links
A string is just that, a string. A URL has different rules as to what characters are allowed and what needs to be encoded (escaped). By setting the type to URI for links, you allow the Flash element parsing code to take care of it, as simple as that. For example:
public function set captionLink( link:String ) {
__linkText = caption;
}
[Inspectable(type='URI', defaultValue='http://www.macromedia.com' )]
public function get captionText():String {
return __captionText;
}
This example is actually not taken from the footer nav component.
Note: This is a setting you use for the actual links only. Descriptions or a target window are not part of the URL, and thus keep their type as string.
Defining the Type for Array Elements
When you define Array elements in Flash using the component parameters, Flash isn't very picky about their type. The elements are not typed, which means they are actually strings, but you can use them as numbers if the value happens to be a number.
By defining a type for Array elements, you can choose a type and the parser in the Flash element will convert the array elements to that type. For example:
public function set buttonLabels( labels:Array ):Void {
__labels = labels;
invalidate();
}
[Inspectable( type='Array', defaultValue='Macromedia,Google,Yahoo,Nirvana' )]
[ArrayProps(elementType='String')]
public function get buttonLabels():Array {
return __labels;
}
Further Documentation
For more info on component meta data in general, see www.macromedia.com/devnet/mx/flash/articles/buildtest_comp_05.html or check the help in Macromedia Flash MX 2004: Using Components > Creating Components > Component meta data.
You will also find additional information on the structure and elements of a SWC file in the help files in Macromedia Flash MX 2004: Using Components > Creating Components > Exporting the component > Understanding SWC files.
Note: You may have to update your help files in order to see this content in Flash MX 2004.
Updating the FooterNav Component
With the instructions above, you can make all the necessary changes to the meta data in the FooterNav.as class file. For your reference, here is how it should look:
class FooterNav extends mx.core.UIComponent
{
var className:String = "FooterNav";
static var symbolOwner:Object = FooterNav;
static var symbolName:String = "FooterNav";
private var __buttonStyles:Object = {
color : 0x2B333C
,fontSize : 9
,fontFamily : "Verdana"
,fontWeight : "bold"
}
private var __captionStyles:Object = {
color : 0x2B333C
,fontSize : 9
,fontFamily : "Verdana"
,fontWeight : "normal"
}
private var __borderWidth:Number = 2;
private var __buttonAlign:String;
private var __labels:Array;
private var __links:Array;
private var __linkTarget:String;
private var __captionText:String;
private var __captionAlign:String;
private var __margin:Number = 5;
private var __spacing:Number = 2;
private var captionLabel:MovieClip;
private var bg_mc:MovieClip;
private var bgBorder_mc:MovieClip;
private var container_mc:MovieClip;
var clipParameters:Object = { buttonLinks:1, buttonLabels:1, buttonLinkTarget:1, buttonAlignment:1, captionText:1, captionAlignment:1 };
function FooterNav()
{
trace( "new FooterNav" );
}
// :: inspectable properties ::
public function set buttonAlignment( align:String ) {
__buttonAlign = align;
invalidate();
}
[Inspectable( enumeration='left,center,right', defaultValue='left' )]
public function get buttonAlignment():String {
return __buttonAlign;
}
public function set buttonLabels( labels:Array ):Void {
__labels = labels;
invalidate();
}
[Inspectable( type='Array', defaultValue='Macromedia,Google,Yahoo,Nirvana' )]
[ArrayProps(elementType='String')]
public function get buttonLabels():Array {
return __labels;
}
public function set buttonLinks( links:Array ) {
__links = links;
}
[Inspectable(type='Array', defaultValue='http://macromedia.com,http://google.com,http://yahoo.com' )]
[ArrayProps(elementType='URI')]
public function get buttonLinks():Array {
return __links;
}
public function set buttonLinkTarget( tgt:String ) {
__linkTarget = tgt;
}
[Inspectable(enumeration='_blank,_self,_top,_parent', defaultValue='_blank' )]
public function get buttonLinkTarget():String {
return __linkTarget;
}
public function set captionText( caption:String ) {
__captionText = caption;
invalidate();
}
[Inspectable(type='String', defaultValue='caption goes here' )]
public function get captionText():String {
return __captionText;
}
public function set captionAlignment( align:String ) {
__captionAlign = align;
invalidate();
}
[Inspectable( enumeration='left,center,right', defaultValue='left' ) ]
public function get captionAlignment():String {
return __captionAlign;
}
// :: methods ::
function createChildren(Void):Void
{
// create caption label
attachMovie("Label", "captionLabel", 2);
captionLabel.autoSize = "left";
}
function draw(Void):Void
{
// set up the caption label
for (var style in __captionStyles ) {
captionLabel.setStyle( style, __captionStyles[ style ] );
}
captionLabel.text = __captionText;
// create buttons
var container:MovieClip = this.createEmptyMovieClip( "container_mc", 0 );
var item:MovieClip;
var len:Number = __labels.length;
for ( var i:Number=0; i<len; i++ ) {
item = container.attachMovie( "Button", "item"+i, i );
// set button styles
for (var style in __buttonStyles ) {
item.setStyle( style, __buttonStyles[ style ] );
}
item.label = __labels[ i ];
if ( __links[ i ] != undefined ) {
item.data = __links[ i ];
}
else {
item.enabled = false;
}
item.fitContents();
item.addEventListener("click", handleEvent);
}
size();
}
function size(Void):Void
{
trace( "size" );
doLayout();
super.size();
}
function doLayout():Void
{
// place buttons
var container:MovieClip = this.container_mc;
var item:MovieClip;
var len:Number = __labels.length;
var xPos:Number = __margin;
var yPos:Number = __margin;
for ( var i:Number=0; i<len; i++ ) {
item = container[ "item"+i ];
item.move( xPos, yPos );
xPos += item.width + __spacing;
}
// align buttons
if ( __buttonAlign == "left" ) {
container._x = 0;
}
else if ( __buttonAlign == "center" ) {
container._x = Math.floor( ( __width - xPos ) / 2 );
}
else if ( __buttonAlign == "right" ) {
container._x = __width - xPos;
}
// place label
var x:Number;
var y:Number;
y = __height - captionLabel.height - __margin + 4; // offset for extra height of label
if ( __captionAlign == "center" ) {
x = Math.floor ( ( __width - captionLabel.width) /2);
}
else if ( __captionAlign == "right" ) {
x = Math.floor( __width - captionLabel.width - __margin );
}
else {
x = __margin;
}
captionLabel.move ( x, y );
// size and place background and border
bg_mc._x = bg_mc._y = __borderWidth;
bg_mc._width = __width - __borderWidth*2;
bg_mc._height = __height - __borderWidth*2;
bgBorder_mc._x = bgBorder_mc._y = 0;
bgBorder_mc._width = __width;
bgBorder_mc._height = __height;
}
function handleEvent( event_obj ):Void
{
var link:String = event_obj.target.data;
if ( link != null && link != "" ) {
getURL( link, __linkTarget );
}
}
}
Exporting the FooterNav Component as Flash Element
The process of exporting a Flash Element is actually no different from exporting a component for placement in the component panel. All you need to do is export it as a SWC file. You just don't copy it into the folder for the components panel.
- Open FooterNav.fla.
- Open the library.
- Right-click (on Mac, Control-click) the FooterNav component symbol.
- From the menu, choose Export to SWC File.
- Choose FooterNav.swc as the name and click OK.